#include "editdialog.h"
#include "ui_editdialog.h"

EditDialog::EditDialog(const QSqlDatabase &db, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::EditDialog)
{
    ui->setupUi(this);

    _db=db;
}

EditDialog::~EditDialog()
{
    delete ui;
}

void EditDialog::prepareStatement(const QMap<QString, QString> &map)
{
    statements=map;

    if(statements.contains("entry"))
    {
        auto entry=statements.value("entry");
        setWindowTitle(entry + " - " + "Edit");
    }
}

void EditDialog::transNameByCode(const QString &code,
                                 const QString &getNameByCode,
                                 QLineEdit &nameEdit)
{
    if(statements.contains(getNameByCode))
    {
        auto sql=statements.value(getNameByCode).arg(code);
        transferLog(QString("[%1] %2")
                    .arg(getNameByCode).arg(sql));

        QSqlQuery query(_db);
        query.exec(sql);
        if(query.next())
        {
            auto name=query.value(0).toString();
            nameEdit.setText(name);
        }
        else
            appendMessage("查询不到相关条目");
    }
    else
        appendMessage(QString("当前功能缺失或者不提供该功能[%1]").arg(getNameByCode));
}

void EditDialog::transCodeByName(const QString &name,
                                 const QString &getCodeByName,
                                 QLineEdit &codeEdit)
{
    if(statements.contains(getCodeByName))
    {
        auto sql=statements.value(getCodeByName).arg(name);
        transferLog(QString("[%1] %2")
                    .arg(getCodeByName).arg(sql));

        QSqlQuery query(_db);
        query.exec(sql);
        if(query.next())
        {
            auto code=query.value(0).toString();
            codeEdit.setText(code);
        }
        else
            appendMessage("查询不到相关条目");
    }
    else
        appendMessage(QString("当前功能缺失或者不提供该功能[%1]").arg(getCodeByName));
}

void EditDialog::appendMessage(const QString &message)
{
    ui->reply->append(message);
}

void EditDialog::on_btn_query_clicked()
{
    auto busCode=ui->busCode->text().trimmed();
    if(busCode.isEmpty())
    {
        appendMessage("单据编号不能为空！");
        return;
    }

    if(statements.contains("queryByCode") && statements.contains("columns"))
    {
        auto sql=statements.value("queryByCode").arg(busCode);
        transferLog(QString("[%1] %2")
                    .arg("query").arg(sql));

        QSqlQuery query(_db);
        query.exec(sql);
        if(query.next())
        {
            auto items=statements.value("columns").split("，");
            QStringList summary;
            for(int i=0;i < items.size();i++)
            {
                summary.append(QString("%1: %2")
                               .arg(items.at(i))
                               .arg(query.value(i).toString()));
            }
            appendMessage(summary.join("\n"));
        }
        else
            appendMessage("查询不到相关条目");
    }
    else
        appendMessage("当前功能缺失或者不提供该功能[query]");
}

void EditDialog::on_btn_edit_clicked()
{
    auto busCode=ui->busCode->text().trimmed();
    if(busCode.isEmpty())
    {
        appendMessage("单据编号不能为空！");
        return;
    }

    auto personCode=ui->personCode->text().trimmed();
    auto depCode=ui->depCode->text().trimmed();

    if(personCode.isEmpty() && depCode.isEmpty())
    {
        appendMessage("业务员编码、部门编码不能为空！");
        return;
    }

    if(statements.contains("queryCode"))
    {
        auto sql=statements.value("queryCode").arg(busCode);
        transferLog(QString("[%1] %2")
                    .arg("queryCode").arg(sql));

        QSqlQuery query(_db);
        query.exec(sql);
        if(query.next())
        {
            auto cPersonCode=query.value(0).toString();
            auto cDepCode=query.value(1).toString();
            if(personCode.isEmpty())
                personCode=cPersonCode;

            if(depCode.isEmpty())
                depCode=cDepCode;

            updateAction(personCode,depCode,busCode);
        }
        else
            appendMessage("查询不到原始单据");
    }
}

void EditDialog::updateAction(const QString &personCode,
                              const QString &depCode,
                              const QString &busCode)
{
    if(personCode.isEmpty() || depCode.isEmpty() || busCode.isEmpty())
        return;

    if(statements.contains("update"))
    {
        auto sql=statements.value("update")
                .arg(personCode)
                .arg(depCode)
                .arg(busCode);

        transferLog(QString("[%1] %2")
                    .arg("update").arg(sql));

        QSqlQuery query(_db);
        if(query.exec(sql))
            appendMessage("修改完成，请查询验证");
        else
            appendMessage("无法操作，错误信息： " + query.lastError().text());
    }
    else
        appendMessage("当前功能缺失或者不提供该功能[update]");
}

// Name转Code
void EditDialog::on_btn_personTransL_clicked()
{
    auto name=ui->personName->text().trimmed();
    if(name.isEmpty())
        return;

    transCodeByName(name,"refPersonCode",*(ui->personCode));
}

// Code转Name
void EditDialog::on_btn_personTransR_clicked()
{
    auto code=ui->personCode->text().trimmed();
    if(code.isEmpty())
        return;

    transNameByCode(code,"refPersonName",*(ui->personName));
}

// Name转Code
void EditDialog::on_btn_depTransL_clicked()
{
    auto name=ui->depName->text().trimmed();
    if(name.isEmpty())
        return;

    transCodeByName(name,"refDepCode",*(ui->depCode));
}

// Code转Name
void EditDialog::on_btn_depTransR_clicked()
{
    auto code=ui->depCode->text().trimmed();
    if(code.isEmpty())
        return;

    transNameByCode(code,"refDepName",*(ui->depName));
}
